package com.fortune.service.impl;

import com.fortune.dao.PaymentMgtDao;
import com.fortune.dto.in.paymentMgt.DistributorPaymentAddDto;
import com.fortune.dto.in.paymentMgt.DistributorPaymentEditDto;
import com.fortune.dto.in.paymentMgt.DistributorPaymentListDto;
import com.fortune.dto.in.paymentMgt.OutPayId;
import com.fortune.exception.db.DeleteInnerErrorException;
import com.fortune.exception.db.InsertInnerErrorException;
import com.fortune.service.PaymentMgtService;
import com.fortune.util.UUIDUtil;
import com.fortune.vo.paymentMgt.DistributorPayment;
import com.fortune.vo.paymentMgt.DistributorPaymentDetail;
import com.fortune.vo.paymentMgt.OutPay;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Administrator on 2018/5/29 0029.
 */
@Service
public class PaymentMgtServiceImpl implements PaymentMgtService {
    @Autowired
    private PaymentMgtDao dao;
    @Override
    public List<DistributorPayment> findDistributorPaymentList(DistributorPaymentListDto dto, String mid, String did) {
        return dao.findDistributorPaymentList(dto,mid,did);
    }

    @Override
    public int findDistributorPaymentListCount(DistributorPaymentListDto dto, String mid, String did) {
        return dao.findDistributorPaymentListCount(dto,mid,did);
    }

    @Override
    public DistributorPaymentDetail findDetail(String mid, String did, String paymentId) {
        DistributorPaymentDetail detail = dao.findDetail(mid,did,paymentId);
        List<OutPay> list = dao.findOutPay(mid,did,detail.getId());
        detail.setOutPay(list);
        return detail;
    }

    @Override
    @Transactional
    public int add(DistributorPaymentAddDto addDto,String mid,String did,String id) {
        int success = dao.add(addDto,mid,did,id);
        if(success > 0){
          List<OutPayId> list = addDto.getOutId();
          for(OutPayId ids: list){
             int update = dao.updateOutPay(mid,did,ids.getOutPayId(),addDto.getPayTypeId());
             if(update <= 0){
                 throw new InsertInnerErrorException("添加失败");
             }
          }
        }
        return success;
    }

    @Override
    @Transactional
    public int update(DistributorPaymentEditDto editDto, String mid, String did) {
        int delete = dao.delete(editDto,mid,did);
        if(delete > 0){
            int success = dao.updateAdd(editDto,mid,did,UUIDUtil.get32UUID());
            if(success > 0 ){
                List<OutPayId> list = editDto.getOutId();
                for(OutPayId ids: list){
                    int update = dao.updateOutPay(mid,did,ids.getOutPayId(),editDto.getPayTypeId());
                    if(update <= 0){
                        throw new InsertInnerErrorException("添加失败");
                    }
                }
                return 1;
            }else{
                throw new InsertInnerErrorException("添加失败");
            }
        }else{
            throw new DeleteInnerErrorException("删除失败");
        }
    }

    @Override
    public int deletePayment(String mid, String did, String paymentId) {
        return dao.deletePayment(mid,did,paymentId);
    }
}
